/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-28
*/
package com.rzico.account.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.model.CardCheckVo;
import com.rzico.account.model.CardExtVo;
import com.rzico.account.service.CardBillService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.account.entity.CardCheck;
import com.rzico.account.service.CardCheckService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 充值卡审核控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-28
 */
@Api(description = "充值卡审核接口")
@RestController
@RequestMapping("/admin/cardCheck")
public class CardCheckController extends BaseController {

    @Autowired
    private CardCheckService cardCheckService;


    @Autowired @Lazy
    private SysSequenceService sysSequenceService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private CardBillService cardBillService;

    /**
     * 分页查询充值卡审核
     *
     * @return
    */
    @ApiOperation("分页查询充值卡审核")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "memo", value = "卡备注", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "code", value = "卡审核批次号", dataType = "String", paramType = "query")
    })
    public CommResult<CardCheck> list(String memo, String code, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();

        if (StringUtils.isNotEmpty(memo)) {
            params.put("memo", memo);
        }
        if (StringUtils.isNotEmpty(code)) {
            params.put("code", code);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CardCheck> list = cardCheckService.selectList(params);
        PageResult<CardCheck> pageResult = new PageResult<CardCheck>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询未审批的卡审核批次号
     *
     * @return
     */
    @ApiOperation("查询未审批的卡审核批次号")
    @PostMapping("/selectUnCheckCode")
    @ApiImplicitParams({@ApiImplicitParam(name = "companyId", value = "企业Id", dataType = "Long", paramType = "query")
    })
    public CommResult<CardCheckVo> selectUnCheckCode(Long companyId){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companyId", companyId);
        params.put("state", 0);
        List<String> resultList = cardCheckService.selectUnCheckCode(params);

        return CommResult.success(resultList);
    }

    /**
     * 查询卡审核批次号充值信息
     *
     * @return
     */
    @ApiOperation("查询卡审核批次号充值信息")
    @PostMapping("/selectUploadInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "卡审核批次号", dataType = "String", paramType = "query")
    })
    public CommResult<CardCheckVo> selectUploadInfo(String code){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(code)) {
            params.put("code", code);
        }
        CardCheckVo result = cardCheckService.selectUploadInfo(params);

        return CommResult.success(result);
    }

    /**
     * 确认充值
     *
     * @return
     */
    @ApiOperation("确认充值")
    @PostMapping("/confirmUpload")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "卡审核批次号", dataType = "String", paramType = "query")
    })
    public CommResult<CardCheck> confirmUpload(String code){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(code)) {
            params.put("code", code);
        }
        Map<String, Object> result = cardCheckService.confirmUpload(params, enterprise);

        return CommResult.success(result);
    }

    /**
     * 取消充值
     *
     * @return
     */
    @ApiOperation("取消充值")
    @PostMapping("/cancelUpload")
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "卡审核批次号", dataType = "String", paramType = "query")
    })
    public CommResult<CardCheck> cancelUpload(String code){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(code)) {
            params.put("code", code);
        }
        List<CardCheck> resultList = cardCheckService.cancelUpload(params);

        return CommResult.success(resultList);
    }

    /**
     * 企业员工充值
     *
     * @return
     */
    @ApiOperation("充值申请")
    @PostMapping("/uploadApply")
    public CommResult<CardCheck> uploadApply(@RequestBody List<CardExtVo> list, Long companyId, Integer cardType, String memo,
                                                   String startDate, String endDate, Long[] tagIds){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String,Object> result = cardBillService.checkUploadInfo(list, enterprise.getId(), companyId, cardType, memo, startDate, endDate);
        if (result.get("returnCode").equals("success")) {
            String code = sysSequenceService.generate("cardCheck");
            cardCheckService.uploadApply(list, enterprise.getId(), companyId, cardType, code, memo, startDate, endDate, tagIds, enterprise.getMchId());
        }

        return CommResult.success(result);
    }



    /**
     * 查询单条充值卡审核
     *
     * @return
    */
    @ApiOperation("查询单条充值卡审核")
    @GetMapping("/find/{id}")
    public CommResult<CardCheck> find(@PathVariable String id) {
        CardCheck result = cardCheckService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("cardCheck",result);
        return CommResult.success(data);

    }

    /**
     * 保存充值卡审核
     *
     * @param cardCheck
     * @return
     */
    @ApiOperation("保存充值卡审核")
    @PostMapping("/save")
    public CommResult<CardCheck> save(@RequestBody CardCheck cardCheck) {
        int affectCount = cardCheckService.insert(cardCheck);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        CardCheck result = cardCheckService.findById(cardCheck.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除充值卡审核
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除充值卡审核", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除充值卡审核,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<CardCheck> del(@PathVariable String ids) {

        cardCheckService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新充值卡审核
     *
     * @param cardCheck
     * @return
     */
    @Log(desc = "更新充值卡审核", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新充值卡审核")
    @PostMapping("/update")
    public CommResult<CardCheck> update(@RequestBody CardCheck cardCheck) {
        int affectCount = cardCheckService.updateByPrimaryKeySelective(cardCheck);
        if (affectCount <= 0){
            return CommResult.error();
        }
        CardCheck result = cardCheckService.findById(cardCheck.getId());
        return CommResult.success(result);
    }

}
